import storage from 'store'
import { AllState } from '../index'
import { ActionContext } from 'vuex'
import { message } from 'ant-design-vue'
import { LoginFrom } from '@/types/views/login'
import { RouterTable } from '@/types/api/login'
import { generator } from '@/utils/parsingRouter'

import { login, info, menu, logout,areaadd,areaedit,areadel,arealist,areaone,
postadd,postdel,postedit,postlist,postone,majoradd,majordel,majoredit,majorlist,majorone,
comlist,comadd,comdel,comedit,comone } from '@/api/login'

// 处理用户登录、登出、个人信息、权限路由

export type UserState = {
  token: string,
  name: string,
  avatar: string,
  roles: string[],
  routers?: RouterTable
}

const state: UserState = {
  // 标识
  token: storage.get('token'),
  // 昵称
  name: '',
  // 头像
  avatar: '',
  // 角色(鉴权)
  roles: [],
  // 路由表(原始未解析)
  routers: []
}

const user = {

  namespaced: true,

  state,

  mutations: {

    // 设置token
    setToken (state: UserState, token: string) {
      state.token = token
    },
    
    // 设置用户信息
    setInfo (state: UserState, info: UserState) {
      const { name, avatar, roles } = info
      state.name = name
      state.avatar = avatar
      state.roles = roles
    },

    // 设置路由表(原始未解析)
    setRouters (state: UserState, routers: RouterTable) {
      state.routers = routers
    },

    // 用户退出登录
    clearState (state: UserState) {
      storage.remove('token')
      // 为了重新加载用户信息及路由组
      state.name = ''
    }
  
  },

  actions: {

    // 登录
    login (context: ActionContext<UserState, AllState>, params: LoginFrom) {
      return new Promise((resolve, reject) => {
        login(params).then(e => {
          const data = e.data
          storage.set('token', data.token)
          context.commit('setToken', data.token)
          resolve(data)
        }).catch(err => {
          reject(err)
        })
      })
    },

    // 获取用户信息
    userInfo (context: ActionContext<UserState, AllState>) {
      return new Promise((resolve, reject) => {
        info().then(e => {
          const info = e.data.info 
          context.commit('setInfo', info)
          resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          if (err.data && err.data.code !== -401) {
            reject(err)
          }
        })
      })
    },

    // 获取菜单
    menu (context: ActionContext<UserState, AllState>) {
      return new Promise((resolve) => {

        const routeTable = [
          {
            name:"用户",
            component:"BasicLayout",
            id:1,
            path:"/",
            key:"users",
            // redirect:"/member/Enterprise_member",
            children:[
                {
                    name:"会员管理",
                    path:"/member",
                    key:"member",
                    component:"RouteView",
                    children:[
                      {
                        name:"企业会员",
                        path:"/member/Enterprise_member",
                        key:"Enterprise_member",
                        component:"/member/Enterprise_member/index",
                      },
                      {
                        name:"个人会员",
                        path:"/member/individual_member",
                        key:"individual_member",
                        component:"/member/individual_member/index",
                      },
                      {
                        name:"无效会员",
                        path:"/member/invalid_member",
                        key:"invalid_member",
                        component:"/member/invalid_member/index",
                      },
                    ]
                },
                {
                  name:"简历管理",
                  path:"/resume",
                  key:"resume",
                  component:"/resume",
                  children:[
                    {
                      name:"全部简历",
                      icon:"LaptopOutlined",
                      path:"/resume/resume_all",
                      key:"resume_all",
                      component:"/resume/resume_all/index",
                    },
                    {
                      name:'简历添加',
                      path:"/resume/resume_add",
                      component:"/resume/resume_add/index",
                      hidden:true,
                    }
                  ]
                },
                {
                  name:"CRM企业管理",
                  path:"/business_management",
                  key:"business_management",
                  component:"/business_management",
                  children:[
                    {
                      name:"全部线索",
                      path:"/business_management/clue_all",
                      key:"clue_all",
                      component:"/business_management/clue_all/index",
                    },
                    {
                      name:"我的线索",
                      path:"/business_management/clue_my",
                      key:"clue_my",
                      component:"/business_management/clue_my/index",
                    },
                    {
                      name:"线索公海",
                      path:"/business_management/clue_public",
                      key:"clue_public",
                      component:"/business_management/clue_public/index",
                    },
                    {
                      name:"所有客户",
                      path:"/business_management/customer_all",
                      key:"customer_all",
                      component:"/business_management/customer_all/index",
                    },
                    {
                      name:"我的客户",
                      path:"/business_management/customer_my",
                      key:"customer_my",
                      component:"/business_management/customer_my/index",
                    },
                    {
                      name:"客户公海",
                      path:"/business_management/customer_public",
                      key:"customer_public",
                      component:"/business_management/customer_public/index",
                    },
                  ]
                },
                {
                  name:"职位信息管理",
                  path:"/Position_management",
                  key:"Position_management",
                  component:"RouteView",
                  children:[
                    {
                      name:"职位管理",
                      path:"/Position_management/position",
                      key:"position",
                      component:"/Position_management/position/index",
                      
                    },
                    {
                      name:"职位修改",
                      hidden:true,
                      path:"/Position_management/position/put",
                      key:"positionPut",
                      component:"/Position_management/put_position/index",
                    }
                  ]
                },
            ]
          },
          {
            name:"内容管理",
            component:"BasicLayout",
            id:2,
            path:"/content",
            key:"content",
            redirect:"/advertisement/Advertising_space",
            children:[
              {
                name:"广告管理",
                path:"/advertisement",
                key:"advertisement",
                component:"/advertisement/Advertising_space",
                children:[
                  {
                    name:"广告位",
                    path:"/advertisement/Advertising_space",
                    key:"Advertising_space",
                    component:"/advertisement/Advertising_space/index",
                  },
                  {
                    name:"广告列表",
                    path:"/Advertising_list",
                    key:"Advertising_list",
                    component:"/advertisement/Advertising_list/index",
                  },
                ] 
              }
            ]
          },

          {
            name:"系统设置",
            component:"BasicLayout",
            id:3,
            path:"/system",
            key:"system",
            children:[
              {
                name:"分类配置",
                path:"/classification",
                key:"classification",
                component:"/classification",
                children:[
                  {
                    name:"地区分类",
                    path:"/classification/region",
                    key:"region",
                    component:"/classification/region/index",
                  },
                  {
                    name:"职位分类",
                    path:"/classification/position_type",
                    key:"position_type",
                    component:"/classification/position_type/index",
                  },
                  {
                    name:"专业分类",
                    path:"/classification/major_type",
                    key:"major_type",
                    component:"/classification/major_type/index",
                  },
                ] 
              },
              {
                name:"系统设置",
                path:"/system_management",
                key:"system_management",
                component:"/system_management",
                children:[
                  {
                    name:"管理员列表",
                    path:"/system_management/admin_list",
                    key:"admin_list",
                    component:"/system_management/admin_list/index",
                  },
                  {
                    name:"管理员列表添加",
                    hidden:true,
                    path:"/system_management/AddAdminList",
                    key:"add_admin_list",
                    component:"/system_management/admin_list/add",
                  },
                  {
                    name:"管理员列表修改",
                    hidden:true,
                    path:"/system_management/PutAdminList",
                    key:"put_admin_list",
                    component:"/system_management/admin_list/put",
                  },
                  {
                    name:"角色权限设置",
                    path:"/system_management/role_qx",
                    key:"role_qx",
                    component:"/system_management/role_qx/index",
                  },
                  {
                    name:"角色权限设置添加",
                    hidden:true,
                    path:"/system_management/role_qx_add",
                    key:"role_qx_add",
                    component:"/system_management/role_qx/add",
                  },
                  {
                    name:"角色权限设置修改",
                    hidden:true,
                    path:"/system_management/role_qx_put",
                    key:"role_qx_put",
                    component:"/system_management/role_qx/put",
                  },
                ] 
              }
            ]
          },
  
        ]

        context.commit('setRouters', routeTable)
        // 初始化侧边菜单
        // context.rootState.menu.menuRouter = routeTable[0]['children'] || []
        // context.rootState.menu.menuId = routeTable[0]['id']
        resolve(generator(routeTable))


        // menu().then(e => {
          // const routeTable = e.data.data
          // console.log(routeTable,'11111')
          // context.commit('setRouters', routeTable)
          // // 初始化侧边菜单
          // context.rootState.menu.menuRouter = routeTable[0]['children'] || []
          // context.rootState.menu.menuId = routeTable[0]['id']
          // resolve(generator(routeTable))
        // }).catch(err => {
        //   message.error(err.message || err.data.message)
        // })
      })
    },

    // 退出登录
    logout (context: ActionContext<UserState, AllState>) {
      return new Promise((resolve, reject) => {
        logout().then(e => {
          context.commit('clearState')
          resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
    // 地区列表
    arealist (context: ActionContext<UserState, AllState>) {
      return new Promise((resolve, reject) => {
        arealist().then(e => {
            resolve(e.data)
        })
      })
    },  
     // 企业会员
    getcomlist (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
       comlist(params).then(e => {
          resolve(e.data)
        }).catch(err => {
          
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 企业添加
     addcom (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
       comadd(params).then(e => {
          resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },

    // 删除地区
    areadel (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        areadel(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 添加地区
     areaadd (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        areaadd(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 获取地区详情
     areaone (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        areaone(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 获取地区详情
     areaedit (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        areaedit(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
    // 职位
    // 职位列表
    postlist (context: ActionContext<UserState, AllState>) {
      return new Promise((resolve, reject) => {
        postlist().then(e => {
            resolve(e.data)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
    // 删除职位
    postdel (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        postdel(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 添加职位
     postadd (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        postadd(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 获取职位详情
     postone (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        postone(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 获取职位详情
     postedit (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        postedit(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
    // 专业
     // 专业列表
     majorlist (context: ActionContext<UserState, AllState>) {
      return new Promise((resolve, reject) => {
        majorlist().then(e => {
            resolve(e.data)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
    // 删除专业
    majordel (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        majordel(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 添加专业
     majoradd (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        majoradd(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 获取专业详情
     majorone (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        majorone(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },
     // 编辑专业
     majoredit (context: ActionContext<UserState, AllState>,params:any) {
      return new Promise((resolve, reject) => {
        majoredit(params).then(e => {
            resolve(e)
        }).catch(err => {
          message.error(err.message || err.data.message)
          reject(err)
        })
      })
    },

    // 企业删除

    delcom (context: ActionContext<UserState, AllState>,params:any) {
     return new Promise((resolve, reject) => {
      comdel(params).then(e => {
         resolve(e)
       }).catch(err => {
         message.error(err.message || err.data.message)
         reject(err)
       })
     })
   },
   // 企业编辑
   editcom (context: ActionContext<UserState, AllState>,params:any) {
    return new Promise((resolve, reject) => {
     comedit(params).then(e => {
        resolve(e)
      }).catch(err => {
        message.error(err.message || err.data.message)
        reject(err)
      })
    })
  },
  // 企业详情
  onecom (context: ActionContext<UserState, AllState>,params:any) {
   return new Promise((resolve, reject) => {
    comone(params).then(e => {
       resolve(e)
     }).catch(err => {
       message.error(err.message || err.data.message)
       reject(err)
     })
   })
 }

  }

}

export default user